﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Linq;

namespace GroupMeal.Foods
{
    public class FoodTypeAppService (IRepository<FoodType,Guid> foodTypeRepository,
                                     AsyncQueryableExecuter queryableExecuter,
                                     IMapper mapper): ApplicationService, IFoodTypeAppService, ITransientDependency
    {
        private readonly IMapper _mapper = mapper;
        private readonly AsyncQueryableExecuter _queryableExecuter= queryableExecuter;
        private readonly IRepository<FoodType,Guid> _foodTypeRepository = foodTypeRepository;

        /// <summary>
        /// 获取详情
        /// </summary>
        public async Task<BackJson> GetDetail(Guid id)
        {
            var foodType = await _foodTypeRepository.FirstOrDefaultAsync(f=>f.Id == id);
            if (foodType == null)
            {
                throw new BusinessException(CustomErrorCodes.FoodTypeNotExist);
            }
            return new BackJson
            {
                code = 0,
                data= _mapper.Map<FoodType,FoodTypeDto>(foodType)
            };
        }

        /// <summary>
        /// 新建
        /// </summary> 
        public async Task<BackJson> Create(CreateFoodTypeDto dto)
        {
            var entity = await _foodTypeRepository.FirstOrDefaultAsync(f => f.Name.Contains(dto.Name));
            if (entity != null)
            {
                throw new BusinessException(CustomErrorCodes.FoodTypeWithTheSameNameExisted);
            }

            entity = _mapper.Map<CreateFoodTypeDto, FoodType>(dto);
            await _foodTypeRepository.InsertAsync(entity);

            return new BackJson
            {
                code = 0,
                msg = "新建成功"
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        public async Task<BackJson> Delete(Guid id)
        {
            var entity = await _foodTypeRepository.FirstOrDefaultAsync(f=>f.Id==id);
            if (entity != null)
            { 
                await _foodTypeRepository.DeleteAsync(entity);
            }
            return new BackJson
            {
                code = 0,
                msg = "删除成功"
            };
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<BackJson> Edit(FoodTypeDto dto)
        {
            var entity = await _foodTypeRepository.FirstOrDefaultAsync(f => f.Id == dto.Id!);
            if (entity==null)
            {
                throw new BusinessException(CustomErrorCodes.FoodTypeNotExist);
            }
            _mapper.Map<FoodTypeDto, FoodType>(dto, entity);
            await _foodTypeRepository.UpdateAsync(entity);
            return new BackJson
            {
                code = 0,
                msg = "保存成功"
            };
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        public async Task<BackJson> GetList(string? filterName)
        {
            var query = await _foodTypeRepository.GetQueryableAsync();
            if (!string.IsNullOrEmpty(filterName))
            {
                query = query.Where(t => t.Name.Contains(filterName));
            }

            var list = await _queryableExecuter.ToListAsync(query);
            return new BackJson
            {
                code = 0,
                data = _mapper.Map<List<FoodType>, List<FoodTypeDto>>(list)
            };
        }
    }
}
